import Vue from 'vue'
import Vuex from 'vuex'
import {getRequest,postRequest} from '@/api/request'

Vue.use(Vuex)

let store=new Vuex.Store({
  state: {
    collapsed:false,//左侧导航默认折叠状态
    leftNavState:'affair',
    address: [{
    value: 'cq',
    label: '重庆',
    children: [{
      value: 'ybq',
      label: '渝北区',
      children: [{
        value: 'zldd',
        label: '竹林大道'
      }, {
        value: 'xgdd',
        label: '星光大道'
      }]
    }, {
      value: 'jbq',
      label: '江北区',
      children: [{
        value: 'zldd',
        label: '竹林大道'
      }, {
        value: 'xgdd',
        label: '星光大道'
      }]
    }]
  }, {
    value: 'sichuan',
    label: '四川省',
    children: [{
      value: 'dy',
      label: '德阳市',
      children:[{
        value: 'zldd',
        label: '竹林大道',
        children:[{
          value: 'xgdd',
          label: '星光大道',
          children:[{
            value: 'zldd',
            label: '竹林大道',
          }]
        }]
      }]
    }, {
      value: 'my',
      label: '绵阳市'
    }]
  }],
    selectedAddress: [], 
    treeUrl:'/api/menu/tree',
    dropTreeUrl:'/api/rcs/sysRegion/getAllRegion',
    menuTree:[],
    dropTree:[],
  },
  mutations:{
    saveTreeData(state,res){
      state.menuTree=res
    },
    saveDropTree(state,res){
      state.dropTree=res
    }
  },
  actions: {
    //获取菜单树的数据
    getTreeData({commit,state}){
      getRequest(state.treeUrl)
      .then(res => {
        var data = res.data;
        if (data.status == 200) {
         // console.log(data.data)
          commit('saveTreeData',data.data)
        } else {
          this.$message.error(data.msg);
        }
      })
      .catch(error => {
        //错误信息
        this.$message.error(error);
      });
    },
    getDropTree({commit,state}){
      postRequest(state.dropTreeUrl).then(res => {
        var data = res.data;
        if (data.status == 200) {
          commit('saveDropTree',data.data)
        } else {
          this.$message.error(data.msg);
        }
      }).catch(error => {
        this.$message.error(error);
      });
    }
  }
})
export default store